package com.linchuansi.juc;

import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.*;

/**
 * *
 *
 * @author wb
 * @date 2025-11-15 22:02*@since 1.0.0
 */
public class CountDownLatchDemo {
    static CountDownLatch countDownLatch = new CountDownLatch(2);
    static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
    volatile int i=5;
    public static void main(String[] args) throws InterruptedException {
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();
        //countDownLatchDemo.async();
        //countDownLatchDemo.sync();
       // fixedThreadPool.shutdown();
        Person person = new Person("linchuansi", 18);
        System.out.println(person.getName());
        System.out.println(person.getOther());
        countDownLatchDemo.demoTask();
        RateLimiter rateLimiter = RateLimiter.create(10);
        rateLimiter.tryAcquire();
    }
    public void demoTask()
    {
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();
        fixedThreadPool.submit(countDownLatchDemo::task1);
        countDownLatchDemo.loop();
        //fixedThreadPool.shutdown();
    }
    public void loop()
    {
        for(;;)
        {
            System.out.println("开始执行");
            if(i==10)
            {
                System.out.println("满足条件");
                return;
            }
            System.out.println("结束循环");
        }
    }
    public void task1()
    {
        System.out.println("开始执行子线程1");
        try {
            Thread.sleep(1000);
            i=10;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void sync() throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();
        countDownLatchDemo.test();
        countDownLatchDemo.test1();
        System.out.println("结束程序");
        System.out.println("程序运行时间：" + (System.currentTimeMillis() - start));

    }

    public void async() throws InterruptedException {
        long start = System.currentTimeMillis();
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();
        fixedThreadPool.submit(countDownLatchDemo::test);
        fixedThreadPool.submit(countDownLatchDemo::test1);
        countDownLatch.await();
        System.out.println("开始等待");
        Thread.sleep(100);
        countDownLatch.await();
        System.out.println("再次等待");
        Thread.sleep(100);
         countDownLatch.await();
        System.out.println("第三次等待");
        Thread.sleep(100);
        System.out.println("结束程序");
        System.out.println("程序运行时间：" + (System.currentTimeMillis() - start));
    }

    void test() {
        System.out.println("开始执行子线程1");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
        System.out.println("子线程1结束");
    }

    void test1() {
        System.out.println("开始执行子线程2");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        countDownLatch.countDown();
        System.out.println("子线程2结束");
    }
    static  class  Person{
        private String name;
        private int age;
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
        public String getName(){
            return name;
        }
        public int getOther()
        {
            int i=0;
            i=age;
            return i;
        }

    }
}
